package xyz.bali16.application.security;


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.stereotype.Component;
import xyz.bali16.application.core.utils.HttpUtils;

import java.util.Objects;
import java.util.concurrent.ExecutorService;


@Component
public class AuthFailureListener implements ApplicationListener<AbstractAuthenticationFailureEvent>{

    private final ExecutorService executorService;
    //private final LoginLogService loginLogService;

    public AuthFailureListener(@Qualifier("system") ExecutorService executorService) {
        this.executorService = executorService;
    }

    @Override
    public void onApplicationEvent(AbstractAuthenticationFailureEvent auth) {
        String ip = HttpUtils.getRemoteAddress();
        Object username = auth.getAuthentication().getPrincipal();
        String s = String.valueOf(username);
        if(Objects.equals(s, "access-token")){
            return;
        }
        //executorService.execute(()->{
        //    loginLogService.addFailure(String.valueOf(username), ip, auth.getTimestamp(),
        //            auth.getException().getLocalizedMessage());
        //});
    }
}
